package com.wy2.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;

import javax.jws.soap.SOAPBinding.Use;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.wy2.entity.Role;
import com.wy2.entity.User;
import com.wy2.service.RoleService;
import com.wy2.service.UserService;
import com.wy2.util.FenyeUtil;
import com.wy2.util.Md5Utils;
import com.wy2.util.QuanZhongUtil;

@RestController
@RequestMapping("user")
public class UserController {
	@Autowired
    private UserService us;
	
	@Autowired
	private RoleService rs;
	
	/**
	 * 显示所有
	 * @param fenye
	 * @param user
	 * @return
	 */
	@RequestMapping("/all")
	public FenyeUtil<User> getAll( FenyeUtil<User> fenye,User user){
		//把查询所有的参数传到分页工具类
		fenye.setT(user);
		fenye = us.getAll(fenye);
		
		return fenye;
		
	}
	/**
	 * 添加用户信息
	 * @param user
	 * @return
	 * @throws ParseException
	 */
	@RequestMapping("/add")
	public Integer addUser(User user) throws ParseException{
		UUID.randomUUID().toString();
		user.setUid(UUID.randomUUID().toString());//生成uuid的uid
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");//时间日期格式
		String time = sdf.format(new Date());//得到现在时间
		user.setUcreatetime(time);//修改时间为当前时间
		user.setUislockout("否");//默认添加锁定字段为否
		user.setUpsdwrongtime(0);//默认添加错误次数为0
		String uidcard = user.getUidcard();//根据用户id获取身份证号
		System.out.println("身份证号为：" + uidcard);
		String b = "~" + uidcard.substring(uidcard.length() - 6 ,uidcard.length());//设置默认密码为~+身份证后六位
		System.out.println("密码为：" + b);
		String md5Pwd = Md5Utils.md5(b);//使用md5加密
		user.setUpassword(md5Pwd);//添加默认密码
		System.out.println("加密后密码为：" + md5Pwd);
		return us.addUser(user);//调用添加方法
		
	}
	/**
	 * 修改
	 * @param user
	 * @return
	 */
	@RequestMapping("/upd")
	public Integer updUser(User user) {
		return us.updUser(user);
		
	}
	/**
	 * 删除
	 * @param uid
	 * @return
	 */
	@RequestMapping("/del")
	public Integer delUser(String uid) {
		return us.delUser(uid);
	}
	
	/**
	 * 密码重置
	 * @param uid
	 * @return
	 */
	@RequestMapping("/new")
	public Object newPass(String uid) {
		Map<String, Object> map = new HashMap<String, Object>();
		List<User> user = us.newPass(uid);
		User u = user.get(0);
		String nodid = u.getUidcard();
		String b = "~"+nodid.substring(nodid.length() - 6, nodid.length());
//		b = Md5Utils.md5(b);
		b = DigestUtils.md5DigestAsHex(b.getBytes());
		System.out.println(uid + "------------------------------");
		Integer n = us.updateNodid(b,uid);
		if (n > 0) {
			map.put("success", true);
		} else {
			map.put("success", false);
		}
		return map;
	}
	
	
	@PostMapping("islocl/false")
	public String upisLockFalse(String uid) {
		int d = us.upisLockFalse(uid);
		if(d>0) {
			return "success";
		}
		return "fail";
	}
	
	
	@PostMapping("islocl/true")
	public String upisLockTrue(String uid) {
		int d = us.upisLockTrue(uid);
		
		if (d > 0) {
			return "success";
		}
		return "fail";
	}
	/**
	 * 根据用户id查询角色
	 * @param uid
	 * @return
	 */
	@RequestMapping("/getuidfindrole")
	public Object findUser(String uid){
		List<Map<String, Object>> res = new ArrayList<Map<String,Object>>();
		List<Role> data = rs.getRoleByUid(uid);
		for(Role role : data) {
			Map<String, Object> map = new HashMap<>();
			map.put("value", role.getRid());
			res.add(map);
		}
		return res;
	}
	/**
	 * 给用户角色中间表添加数据
	 * @param uid
	 * @param rid
	 */
	@RequestMapping("/addusertorole")
	public void saveUserole(String uid, String rid) {
		System.out.println(uid + "--------");
		System.out.println(rid + "---------");
		rs.saveUserole(uid, rid);
	}
	/**
	 * 给用户角色中间表删除数据
	 * @param uid
	 * @param rid
	 */
	@RequestMapping("/delusertorole")
	public void delUserole(String uid, String rid) {
		System.out.println(uid + "--------");
		System.out.println(rid + "---------");
		rs.delUserole(uid, rid);
	}
	
	
	/**
	 * 判断旧密码是否正确
	 */
	@RequestMapping("/selpwd")
	public Object selectpwd(String uname ,String upassword) {
		Map<String, Object> map = new HashMap<String, Object>();
		/*
		 * //生成盐 String salt = Md5Utils.salt(uname);
		 */
		System.out.println("===原密码===" + upassword);
		//转换成数据库里的密码
		 String  upasswordl= DigestUtils.md5DigestAsHex(upassword.getBytes());
		System.out.println("===加密后的密码====" + upasswordl);
		return us.selectPwd(uname,upasswordl) != null ? 1 : 0 ;
	}
	
	/**
	 * 修改密码
	 */
	@RequestMapping("/updw")
	public Integer editPwd(String uname ,String upassword) {
		//获取修改的密码
		System.out.println("====密码为====" + upassword);
		//新密码加密
		 String  xpassl= DigestUtils.md5DigestAsHex(upassword.getBytes());
		
		 System.out.println("===加密后的密码===" + xpassl);
		return us.updPwd(uname,xpassl);
		
		
	}
	
	/*
	 * @RequestMapping("/updw") public Integer editPwd(String uname, String
	 * upassword, String xpass,String qpass) { System.out.println("==姓名===" +
	 * uname); System.out.println("==原密码===" + upassword);
	 * System.out.println("==新密码===" + xpass); System.out.println("==确认密码===" +
	 * qpass); //登录名生成盐 String yanl = Md5Utils.salt(uname); //转变成数据库里面的加密密码 //
	 * String upasswordl = Md5Utils.md5(yanl + upassword); String upasswordl =
	 * DigestUtils.md5DigestAsHex(upassword.getBytes()); //确认原密码 Integer num
	 * =us.loginsUser(uname,upasswordl); if(num > 0) { if( xpass.equals(qpass)) {
	 * //生成盐 String yan = Md5Utils.salt(uname); //最终生成密码 // String pwds =
	 * Md5Utils.md5(yan + xpass); String pwds =
	 * DigestUtils.md5DigestAsHex(xpass.getBytes()); Integer nums =
	 * us.updatepwd(uname,pwds); if(nums>0) { return 1; }else { return 0; } }else {
	 * System.out.println("两次密码不一致"); return 2; } }else {
	 * System.out.println("原密码不对"); return 3; }
	 * 
	 * }
	 */
	
	/**
	 * 身份证号唯一
	 * @param uidcard
	 * @return
	 */
		@RequestMapping("/cardno")
		public Integer UserCard(String uidcard) {
			
				List<User> list = us.UserCard(uidcard);
				if(list != null && list.size()  >0) {
					return 1;
				}else {
					return 0;
				}
			
		}
		/**
		 * 邮箱唯一
		 * @param uprotectemail
		 * @return
		 */
		@RequestMapping("/email")
		public Integer UserEmail(String uprotectemail) {
			List<User> list = us.UserEmail(uprotectemail);
			if(list != null && list.size()  >0) {
				return 1;
			}else {
				return 0;
			}
		}
		/**
		 * 手机号唯一
		 * @param uprotectmetl
		 * @return
		 */
		@RequestMapping("/metl")
		public Integer UserMetl(String uprotectmetl) {
			List<User> list = us.UserMetl(uprotectmetl);
			if(list != null && list.size()  >0) {
				return 1;
			}else {
				return 0;
			}
		}
		
		/**
		 * 解锁锁定
		 * @param user
		 * @return
		 */
		@RequestMapping("islocl")
		public Integer islocl(User user) {
			return us.islocl(user);
		}
	/**
	 * 根据id获取名字
	 * @param uid
	 * @return
	 */
		@RequestMapping("getByUidUname")
		public User getByUidUname(String uid) {
			return us.getByUidUname(uid);
		}
		
		/**
		 * 权重
		 * @param fenye
		 * @param qz
		 * @return
		 */
		@RequestMapping("/qz")
		public FenyeUtil<QuanZhongUtil> getQz( FenyeUtil<QuanZhongUtil> fenye,QuanZhongUtil qz){
		
			//把查询所有的参数传到分页工具类
			fenye.setT(qz);
			fenye = us.getQz(fenye);
			return fenye;
			
		}
		/**
		 * 修改权重
		 * @param qz
		 * @return
		 */
		@RequestMapping("/updQz")
		public Integer UpdQZ(QuanZhongUtil qz) {
			return us.updQz(qz);
			
		}
	
}
